
// 获取时分秒(传入毫秒数,格式化返回)

let max = Math.max
let floor = Math.floor
let asnumber = parseInt

let formatDate = (time) =>
{
    let date = new Date(time)

    let year = date.getFullYear(),
        month = date.getMonth() + 1,//月份是从0开始的
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();

    month = month > 9 ? month : "0" + month;
    day = day > 9 ? day : "0" + day;
    hour = hour > 9 ? hour : "0" + hour;
    min = min > 9 ? min : "0" + min;
    sec = sec > 9 ? sec : "0" + sec

    let newTime = year + '/' +
        month + '/' +
        day + ' ' +
        hour + ':' +
        min + ':' +
        sec

    return newTime
}

module.exports = {

    downFormat(s)
    {
        let value      = s / 1000
        let secondTime = asnumber(value)// 秒
        let minuteTime = 0// 分
        let hourTime   = 0// 小时
        let dayTime    = 0//天

        if (secondTime > 60)
        {//如果秒数大于60，将秒数转换成整数
            //获取分钟，除以60取整数，得到整数分钟
            minuteTime = asnumber(secondTime / 60)
            //获取秒数，秒数取佘，得到整数秒数
            secondTime = asnumber(secondTime % 60)
            //如果分钟大于60，将分钟转换成小时
            if (minuteTime > 60)
            {
                //获取小时，获取分钟除以60，得到整数小时
                hourTime = asnumber(minuteTime / 60)
                //获取小时后取佘的分，获取分钟除以60取佘的分
                minuteTime = asnumber(minuteTime % 60)
                if (hourTime > 24)
                {
                    dayTime = asnumber(hourTime / 24)
                    hourTime = asnumber(hourTime % 24)
                }
            }
        }

        let result = asnumber(secondTime) <= 9 ? ("0" + asnumber(secondTime)) : "" + asnumber(secondTime)

        if (minuteTime > 0)
        {
            if (asnumber(minuteTime) <= 9)
            {
                result = "0" + asnumber(minuteTime) + ":" + result
            } else
            {
                result = "" + asnumber(minuteTime) + ":" + result
            }
        } 
        else
        {
            result = "00" + ":" + result
        }

        if (hourTime > 0)
        {
            result = "" + asnumber(hourTime) + ":" + result
        }
        if (dayTime > 0)
        {
            result = "" + asnumber(dayTime) + "天" + result
        }
        return result
    },

    formatMS(s)
    {
        s = max(asnumber(s), 0)
        if (s == 0)
        {
            return "00:00"
        }

        s = floor(s / 1000)

        let min  = floor((s % (60 * 60)) / 60)
        let sec  = floor(s % 60)
        let hour = floor((s % (60 * 60 * 24)) / (60 * 60))

        if (hour <= 0)
        {
            hour = ""
        }
        else if (hour < 10)
        {
            hour = "0" + hour + ":"
        }
        else
        {
            hour = hour + ":"
        }

        if (min < 10)
        {
            min = "0" + min
        }
        if (sec < 10)
        {
            sec = "0" + sec
        }

        return hour + min + ":" + sec
    },

    dhms(s)
    {
        s = max(asnumber(s), 0)
        if (s == 0)
        {
            return "00:00"
        }

        s = floor(s / 1000)

        let min  = floor((s % (60 * 60)) / 60)
        let sec  = floor(s % 60)
        let hour = floor((s % (60 * 60 * 24)) / (60 * 60))
        let day  = floor(s / (60 * 60 * 24))

        if (day == 0)
        {
            day = ""
        } 
        else
        {
            day = day + "天 "
        }

        if (hour <= 0)
        {
            hour = ""
        }
        else if (hour < 10)
        {
            hour = "0" + hour + ":"
        }
        else
        {
            hour = hour + ":"
        }

        if (min < 10)
        {
            min = "0" + min
        }

        if (sec < 10)
        {
            sec = "0" + sec
        }
        return day + hour + min + ":" + sec
    },

    timeStamp(strTime, format)
    {
        if (!format)
        {
            format = "%Y/%m/%d %X"
        }
        return formatDate(strTime)
    },

    hms(s)
    {
        let h = floor(s / 3600)
        s = s - h * 60 * 60
        let m = floor(s / 60)
        s = s - m * 60

        return [h, m, s]
    },

    formatString(time)
    {
        let date = new Date(time)
        return date.format("yyyy-MM-dd hh:mm:ss")
    },

    ymd(time, hms)
    {
        const date = new Date(time)

        let y = date.getFullYear()

        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d

        if (!hms)
        {
            return y + '-' + m + '-' + d
        }

        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h

        let minute = date.getMinutes()
        
        let second = date.getSeconds()

        minute = minute < 10 ? ('0' + minute) : minute
        second = second < 10 ? ('0' + second) : second

        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second
    }
}
